package com.tigerlee.candle.activity;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.tigerlee.candle.R;
import com.tigerlee.candle.spreadsheet.ArticleFeedParedData;
import com.tigerlee.candle.spreadsheet.ArticleFeedParser;
import com.tigerlee.candle.spreadsheet.MyHttpRequest;
import com.tigerlee.candle.ui.CustomAdapter;
import com.tigerlee.candle.util.Constants;

public class ArticleListActivity extends ManagerActivity implements OnClickListener, OnItemClickListener{

	
	private ListView mListView;
	private CustomAdapter mCustomAdapter;
	
	private Handler mThreadHandler = new Handler();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		super.startDialog();
		setContentView(R.layout.article_list_activity);		
		
	}
	private void setupView() {
		// TODO Auto-generated method stub
		mListView = (ListView) findViewById(R.id.article_listview);
		mCustomAdapter = new CustomAdapter(this, R.layout.custom_list_row, new ArrayList<String>());
		mCustomAdapter.clear();
		mListView.setAdapter(mCustomAdapter);
		mListView.setOnItemClickListener(this);
		
		TextView mCreateArticle = (TextView) findViewById(R.id.textview_article_list);
		mCreateArticle.setOnClickListener(this);

		mThreadHandler.post(new Runnable() {
			
			public void run() {
				// TODO Auto-generated method stub
				if(startParse() == true){
					setOK(true, null);
					mCustomAdapter.notifyDataSetChanged();
				}else{
					setOK(false, getString(R.string.toast_fail_no_data));
				}
			}
		});
		
		
		
	}
	private boolean startParse(){
		MyHttpRequest mHttpRequests = new MyHttpRequest();
		InputStream mInputStream = null;
		try {
			mInputStream = mHttpRequests.getHttpStream(Constants.mArticleFeedUrl);
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} catch (NullPointerException e) {
			e.printStackTrace();
			return false;
		}
		if (mInputStream != null) {
			ArticleFeedParser mArticleFeedParser = new ArticleFeedParser();
			if (mArticleFeedParser.getParedData(mInputStream) == false) {
				return true;
			}
			return true;
		}
		return false;
	}

	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId() == R.id.textview_article_list){
			Intent i = new Intent(this, CreateArticleActivity.class);
			startActivity(i);
		}
	
	}

	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		// TODO Auto-generated method stub
		Intent i = new Intent(this, ReadArticleActivity.class);
		ArrayList<String> mArticle = new ArrayList<String>();
		mArticle.add(ArticleFeedParedData.mTitleVector.get(position));
		mArticle.add(ArticleFeedParedData.mNameVector.get(position));
		mArticle.add(ArticleFeedParedData.mBodyVector.get(position));
		i.putExtra(Intent.EXTRA_TEXT, mArticle);
		startActivity(i);
	}
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		setupView();
		
	}
}
